% testingData is the data to test 
% trainingData is the data to train
testing_dataset = testingData;
i = 0;
j = 0;
nonspammer = 0;
trainingSize = length(trainingData(:,1));
features = length(trainingData(1,:));
for k = 1:trainingSize,
    if trainingData(k,1) == 1
        nonspammer = nonspammer + 1;
    end 
end
normal_dataset = zeros(nonspammer, features);
robot_dataset = zeros(trainingSize - nonspammer, features);
for k = 1:trainingSize,
    if trainingData(k,1) == 1
        i = i + 1;
        for l = 1:features,
            normal_dataset(i,l) = trainingData(k,l);
        end
    end
    if trainingData(k,1) == -1
        j = j + 1;
        for l = 1:features,
            robot_dataset(j,l) = trainingData(k,l);
        end
    end
end
% Average Calculation
average_norm = mean(normal_dataset);
% Variance Calculation
variance_norm = var(normal_dataset);
average_bot = mean(robot_dataset);
variance_bot = var(robot_dataset);
num_0 = length(normal_dataset(:,1));
num_1 = length(robot_dataset(:,1));
prior_0 = (num_0)/(num_0 + num_1);
prior_1 = (num_1)/(num_0 + num_1);
test_data = testing_dataset;
number_of_samples = length(test_data(:,1));

out_0 = zeros(1, number_of_samples);
out_1 = zeros(1, number_of_samples);

for i = 1:number_of_samples,
    %Calculation of Prior.
    out_0(i) = prior_0;
    out_1(i) = prior_1;
    
    sum = 0;
    norm_dev_multiple = 1;
    %Multiplication of Exponential Values can be seems as their sum :
    %exp(x1) * exp(x2) = exp(x1 + x2)
    for j = 2:features,
       sum = sum - (( test_data(i,j) - average_norm(j) ) * ( test_data(i,j) - average_norm(j) ))/(2 * variance_norm(j));
       %Calculation of sigma1 * sigma2 .. * sigma4 for Non Spammers
       norm_dev_multiple = norm_dev_multiple * sqrt(variance_norm(j));
    end
    %Calculation of Posterior for Normal Profile
    out_0(i) = out_0(i)*exp(sum)/norm_dev_multiple;
    bot_dev_multple  = 1;
    sum = 0;
    for j = 2:features,
        sum = sum - (( test_data(i,j) - average_bot(j) ) * ( test_data(i,j) - average_bot(j) ))/(2 * variance_bot(j));
        %Calculation of sigma1 * sigma2 .. * sigma4 for Spammers
        bot_dev_multple = bot_dev_multple * sqrt(variance_bot(j));
    end
    %Calculation of Posterior for Spammer Profile
    out_1(i) = out_1(i)*exp(sum)/bot_dev_multple;
end

count = 0;
for i = 1:number_of_samples,
    if (out_0(i) > out_1(i) && test_data(i,1) == 1)
        count = count + 1;
    end
    if (out_0(i) < out_1(i) && test_data(i,1) == -1)
        count = count + 1;
    end
end
%Calculating the fraction of Normal Profiles.
percentage = (count * 100) / number_of_samples;
disp('Accuracy : ');
disp(percentage);